package com.hzhc.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzhc.dto.UserDto;
import com.hzhc.entity.ChargeBill;
import com.hzhc.entity.CntrChargeDetail;
import com.hzhc.mapper.ChargeBillMapper;
import com.hzhc.mapper.CntrChargeDetailMapper;
import com.hzhc.service.ChargeBillService;
import com.hzhc.service.CntrChargeDetailService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.utils.TokenUtil;
import com.hzhc.vo.ChargeBillVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 费用账单 服务实现类
 * </p>
 *
 * @author 功不唐捐
 * @since 2024-06-12
 */
@Service
@RequiredArgsConstructor
public class ChargeBillServiceImpl extends ServiceImpl<ChargeBillMapper, ChargeBill> implements ChargeBillService {
    private  final CntrChargeDetailService cntrChargeDetailService;
    private  final CntrChargeDetailMapper cntrChargeDetailMapper;

    @Override
    public ResultUtil<T> addBillRet(HttpServletRequest request, ChargeBillVo vo) {
        boolean b=false;
        if(vo.getId()==null){
            UserDto user = TokenUtil.getUser(request);
            //生成账单信息
            ChargeBill chargeBill = new ChargeBill();
            BeanUtils.copyProperties(vo, chargeBill);
            chargeBill.setUserId(user.getId().intValue());
            chargeBill.setCreateTime(LocalDateTime.now());
            chargeBill.setAccountTypeName("收款");
            chargeBill.setCompanyId(vo.getChargeDetails().get(0).getCompanyId());
            chargeBill.setCompanyName(vo.getChargeDetails().get(0).getCompanyName());
            b = save(chargeBill);
            if (b) {
                for (CntrChargeDetail chargeDetail : vo.getChargeDetails()) {
                    chargeDetail.setChargeBillId(chargeBill.getId());
                    chargeDetail.setChargeType("收款");
                    chargeBill.setPrice(String.valueOf(new BigDecimal(StrUtil.isNotBlank(chargeBill.getPrice()) ? chargeBill.getPrice() : "0").add(new BigDecimal(chargeDetail.getPrice()))));
                }
                cntrChargeDetailService.updateBatchById(vo.getChargeDetails());
                updateById(chargeBill);
            }
        }else{
            ChargeBill bill = baseMapper.selectById(vo.getId());
            if (bill!=null) {
                for (CntrChargeDetail chargeDetail : vo.getChargeDetails()) {
                    chargeDetail.setChargeBillId(bill.getId());
                    chargeDetail.setChargeType("收款");
                    bill.setPrice(String.valueOf(new BigDecimal(StrUtil.isNotBlank(bill.getPrice()) ? bill.getPrice() : "0").add(new BigDecimal(chargeDetail.getPrice()))));
                }
                cntrChargeDetailService.updateBatchById(vo.getChargeDetails());
                b=updateById(bill);
            }
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public ResultUtil<T> backBillRet(HttpServletRequest request, ChargeBillVo vo) {
        ChargeBill bill = baseMapper.selectById(vo.getId());
        boolean b=false;
        if (bill != null) {
            List<CntrChargeDetail> cntrChargeDetails = cntrChargeDetailMapper.selectList(new QueryWrapper<CntrChargeDetail>().eq("disable", 0)
                    .eq("settlement_state", 0).eq("charge_bill_id", bill.getId()));
            if(cntrChargeDetails.size()>0){
                for (CntrChargeDetail chargeDetail : cntrChargeDetails) {
                    chargeDetail.setChargeBillId(null);
                    chargeDetail.setChargeType("");
                    bill.setPrice(String.valueOf(new BigDecimal(StrUtil.isNotBlank(bill.getPrice()) ? bill.getPrice() : "0").subtract(new BigDecimal(chargeDetail.getPrice()))));
                }
                cntrChargeDetailService.updateBatchById(cntrChargeDetails);
            }
            bill.setDisable(true);
            b = updateById(bill);
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }

    @Override
    public List<ChargeBillVo> getChargeBillListPage(ChargeBillVo vo, Page<ChargeBillVo> pagination) {
        return  baseMapper.selectChargeBillList(pagination,vo);
    }

    @Override
    public ResultUtil<T> checkBillRet(HttpServletRequest request, ChargeBillVo vo) {
        ChargeBill bill = baseMapper.selectById(vo.getId());
        boolean b=false;
        if (bill != null) {
            bill.setFinishState(1);
            bill.setUpdateTime(LocalDateTime.now());
            b = updateById(bill);
        }
        return b ? ResultUtil.ok() : ResultUtil.fail();
    }
}
